#include <SDL/SDL.h>


int menu(SDL_Surface * screen)
{
    SDL_Surface *image;
    SDL_Rect src, dest,jouer,charger,option,quiter;//These rectangles will describe the source and destination regions of our blit
    SDL_Event event;
    int done=0;
    int choix=-1;
    int select=-1;

    image = SDL_LoadBMP("image.bmp");

    if (image == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    src.x = 0;
    src.y = 0;
    src.w = image->w;
    src.h = image->h;

    dest.x = 0;
    dest.y = 0;
    dest.w = image->w;
    dest.h = image->h;

    jouer.x = 200;//Display the image at the (X,Y) coordinates (100,100)
    jouer.y = 364;
    jouer.w =154;
    jouer.h = 41;


    charger.x = 200 ;
    charger.y = 421 ;
    charger.w = 154 ;
    charger.h = 39 ;

    option.x = 200 ;
    option.y = 478 ;
    option.w = 154 ;
    option.h = 38 ;


    quiter.x = 200;
    quiter.y = 535 ;
    quiter.w = 154 ;
    quiter.h = 38 ;


    while(!done) //Menu LOOP

    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = 0;
                break;
                // check for keypresses
            case SDL_KEYDOWN:

                if(event.key.keysym.sym == SDLK_ESCAPE)
                    done=1;
                break;
            case SDL_MOUSEBUTTONUP :
                if((event.button.button == SDL_BUTTON_LEFT)&& (event.button.x>jouer.x) && (event.button.x <(jouer.x +jouer.w)) && (event.button.y>jouer.y) &&(event.button.y<(jouer.y+jouer.h)) )
                {
                    choix=0;
                    done=1;
                }
                if((event.button.button == SDL_BUTTON_LEFT)&& (event.button.x>charger.x) && (event.button.x <(charger.x +charger.w)) && (event.button.y>charger.y) &&(event.button.y<(charger.y+charger.h)) )
                {
                    choix=1;
                    done=1;
                }
                if((event.button.button == SDL_BUTTON_LEFT)&& (event.button.x>option.x) && (event.button.x <(option.x +option.w)) && (event.button.y>option.y) &&(event.button.y<(option.y+option.h)) )
                {
                    choix=2;
                    done=1;
                }
                if((event.button.button == SDL_BUTTON_LEFT)&& (event.button.x>quiter.x) && (event.button.x <(quiter.x +quiter.w)) && (event.button.y>quiter.y) &&(event.button.y<(quiter.y+quiter.h)) )
                {
                    choix=3;
                    done=1;
                }

                break;

            case SDL_MOUSEMOTION:
                if(event.motion.x >jouer.x && event.motion.x <jouer.x+jouer.w && event.motion.y > jouer.y && event.motion.y<jouer.y+jouer.h)
                    select=0;
                if(event.motion.x >charger.x && event.motion.x<charger.x+charger.w && event.motion.y > charger.y && event.motion.y<charger.y+charger.h)
                    select=1;
                if(event.motion.x >option.x && event.motion.x <option.x+option.w && event.motion.y > option.y && event.motion.y<option.y+option.h)
                    select=2;
                if(event.motion.x >quiter.x && event.motion.x <quiter.x+quiter.w && event.motion.y > quiter.y && event.motion.y<quiter.y+quiter.h)
                    select=3;
                break;
            } // end switch

        } // end of message processing
        SDL_BlitSurface(image, &src, screen, &dest);

        if(select == 0)
            SDL_FillRect(screen,&jouer,SDL_MapRGB(screen->format, 255, 255, 0));
        if(select == 1)
            SDL_FillRect(screen,&charger,SDL_MapRGB(screen->format, 0, 255, 0));
        if(select == 2)
            SDL_FillRect(screen,&option,SDL_MapRGB(screen->format, 0, 255, 255));
        if (select == 3)
            SDL_FillRect(screen,&quiter,SDL_MapRGB(screen->format, 255, 0, 0));


        //Flip the backbuffer to the primary Hardware Video Memory
        SDL_Flip(screen);


    }


//Release the surface
    SDL_FreeSurface(image);


    return (choix);

}
